package com.woniuxy.shiroinitest;



import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class ShiroPermIniTest {
    public static void main(String[] args) {
        //配置ini shiro环境
        BasicIniEnvironment basicIniEnvironment = new BasicIniEnvironment("classpath:shiroini/shiro-perm.ini");
        //通过环境创建一个SecurityManager
        SecurityManager securityManager = basicIniEnvironment.getSecurityManager();
        //给工具类中注入SecurityManager，表示这个工具类是基于这个环境生成的！
        SecurityUtils.setSecurityManager(securityManager);
        //通过环境生成主体对象
        Subject subject = SecurityUtils.getSubject();
        //主体进行登录，然后SercurityManager内部自己去环境匹配账号密码
        subject.login(new UsernamePasswordToken("lisi","654321"));
        //直接获取SecurityManager登录判断认证结果
        System.out.println("用户登录状态：如果true认证成功"+subject.isAuthenticated());
        //获取登录成功后的用户的名称
        System.out.println(subject.getPrincipals());

        //判断该用户是否拥有某个角色
        System.out.println("判断该主体是否有role角色："+subject.hasRole("role"));
        //判断该用户是否拥有某个权限
        System.out.println("判断该主体是否有user::find权限："+subject.isPermitted("user::find"));
        if(subject.isAuthenticated()){
            //权限的粒度不同!
            if(subject.hasRole("role")){
                System.out.println("该"+subject.getPrincipal()+"有role3角色，用户继续执行页面！");
            }
        }
    }
}
